﻿//    Copyright 2010,2011 Himanshu Vasishth
//   
//    This file is part of SecretVault.
//
//    SecretVault is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SecretVault is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with SecretVault.  If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PasswordStoreProvider;
using System.IO;
using Common;

namespace PasswordLocker.Controls
{
    public partial class GetStoreLocationControl : UserControl
    {
        public GetStoreLocationControl()
        {
            InitializeComponent();
        }

        public event EventHandler NextClicked;
        public event EventHandler CancelClicked;

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string FileLocation
        {
            get
            {
                return Path.GetFullPath(filePathTextBox.Text.Trim());
            }
            set
            {
                filePathTextBox.Text = value;
            }
        }

        private void BrowseButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog selectFileLocationDialog = new SaveFileDialog();
            selectFileLocationDialog.CheckFileExists = false;
            selectFileLocationDialog.DefaultExt = Constants.FileExtension;
            selectFileLocationDialog.FileName = FileLocation;
            selectFileLocationDialog.Filter =
                string.Format("Password Locker Files(*.{0})|*.{0}", Constants.FileExtension);
             
            var dialogResult = selectFileLocationDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                FileLocation = selectFileLocationDialog.FileName;
            }
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            if(CancelClicked != null)
            {
                CancelClicked(this, e);
            }
        }

        private void NextButtonClicked(object sender, EventArgs e)
        {
            errorProvider.Clear();
            // validate input before proceeding
            if (!ValidateChildren())
            {
                return;
            }

            if (NextClicked != null)
            {
                NextClicked(this, e);
            }
        }

        private void FilePathTextBox_Validating(object sender, CancelEventArgs e)
        {
            // A rudimentry check to try and verify the file path is valid. This should catch most 
            // issues though not all
            bool pathInvalid = string.IsNullOrEmpty(filePathTextBox.Text.Trim()) ||
                             filePathTextBox.Text.Split(Path.GetInvalidPathChars()).Length > 1;

            if (pathInvalid)
            {
                errorProvider.SetError(filePathTextBox, "Need to provide a valid path (ph)");
                e.Cancel = true;
            }
        }
    }
}
